<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 头部 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="$elephant" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-tree :data="list" default-expand-all>
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="$panda" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length ===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

        <el-dialog
          :title="isEdit?'编辑部门':'添加子部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="$refs.addOrEditRef.resetForm()"
        >
          <editDialog
            :id="curId"
            ref="addOrEditRef"
            :is-edit="isEdit"
            :origin-list="originList"
            @success="hSuccess"
            @close="showDialog = false"
          />
        </el-dialog>

      </el-card>
    </div>
  </div>
</template>

<script>
import editDialog from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { tranDataToTree } from '@/utils'
export default {
  components: { editDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [], // 部门列表
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: [] // 保存所有部门列表的部分
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 加载组织架构列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('getDepartments', res)
        // 获取部门数据并取出,数组中每个对象的部门数据(id,pid,name,code)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        res.data.depts.shift(1)
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log('error loadDepartments', error)
      }
    },
    // 添加子部门 传递id
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    // 子组件添加成功重新渲染
    hSuccess() {
      this.loadDepartments()
      this.showDialog = false
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.addOrEditRef.loadDepartmentDetail()
      })
    },
    // 删除部门
    hDel(id) {
      this.$confirm('确认删除?', '提示', {
        comifrmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartment(id)
        this.loadDepartments()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>
